#include "EditorScene.h"
#include "Editor.h"

using namespace irr;
using namespace core;
using namespace video;

EditorScene::EditorScene(Editor* editor)
:IEditorComponent(editor),entityMarker(0),smgr(0),camera(0)
{
}

EditorScene::~EditorScene()
{
	camera->drop();
}

void EditorScene::init()
{
	irr::SKeyMap keymap[4]=
	{
		{EKA_STRAFE_LEFT,KEY_KEY_A},
		{EKA_STRAFE_RIGHT,KEY_KEY_D},
		{EKA_MOVE_BACKWARD,KEY_KEY_S},
		{EKA_MOVE_FORWARD,KEY_KEY_W}
	};

	smgr=editor->game->getIrrlichtDevice()->getSceneManager();

	camera=smgr->addCameraSceneNodeFPS(0,100.0f,0.5f,-1,keymap,4);
	camera->setIsDebugObject(true);
	camera->setInputReceiverEnabled(false);
	camera->setName("EditorCamera");//for fun

	camera->grab();
	camera->remove();
	
	driver=smgr->getVideoDriver();
	entityMarker=driver->getTexture("editor/entityMarker.png");
}

void EditorScene::onPreEdit()
{
	oldCam=smgr->getActiveCamera();
	camera->setPosition(oldCam->getPosition());
	camera->setTarget(oldCam->getTarget());
	smgr->setActiveCamera(camera);
	smgr->getRootSceneNode()->addChild(camera);
}

void EditorScene::onPostEdit()
{
	smgr->setActiveCamera(oldCam);
	camera->remove();
}

void EditorScene::render()
{
	smgr->drawAll();
	SGF::IEntity* entity=editor->getSelectedEntity();
	if(entity)
	{
		driver->draw2DImage(entityMarker,
			smgr->getSceneCollisionManager()->getScreenCoordinatesFrom3DPosition(entity->getPosition())
			,rect<s32>(0,0,24,24),0,SColor(255,255,255,255),true);
	}
}